/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.RapPOJO;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Utils.MySqlDataAccessHelper;

/**
 *
 * @author KimLoan
 */
public class RapDAO {
     public boolean themRap(RapPOJO rap) {
        boolean kq = false;
        String sql = String.format("INSERT INTO rap(TenRap, DiaChi,SoLuongPhong)"
                + " VALUES('%s','%s',%d)", rap.getTenRap(),rap.getDiaChi(),rap.getSoLuongPhong());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Them Rap thanh cong
        }

        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xoa Rap
     * @return
     */
    public boolean xoaRap(String maRap) {

        boolean kq = false;
        try {
            String sql = "delete from Rap where idRap=" + maRap;
            // String sql = "DELETE FROM Rap WHERE maRap ='"+  maRap+"'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            int n = helper.executeUpdate(sql);
            if (n >= 1) {
                kq = true;//xoa thanh cong
            }
            helper.close();
            return kq;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return kq;
        }
    }

    /**
     * Phuong Thuc Cap Nhat Rap
     */
    public boolean capNhatRap(RapPOJO rap) {
        boolean kq = false;

        String sql = String.format(
                "UPDATE Rap SET TenRap='%s', DiaChi='%s',SoLuongPhong=%d"
                + " WHERE idRap=%d ", rap.getTenRap(),rap.getDiaChi(),rap.getMaRap());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;//Cap Nhat Rap thanh cong
        }
        helper.close();
        return kq;
    }

    /**
     * Phuong Thuc Xem Danh Rap
     * @return
     */
    public ArrayList<RapPOJO> layDanhRapCacMaRap() {
        ArrayList<RapPOJO> ds = new ArrayList<RapPOJO>();
        try {
            String sql = "SELECT * FROM Rap";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                RapPOJO rap = new RapPOJO();
                rap.setMaRap(rs.getInt("idRap"));
                rap.setTenRap(rs.getString("TenRap"));
                rap.setDiaChi(rs.getString("DiaChi"));
                rap.setSoLuongPhong(rs.getInt("SoLuongPhong"));
                ds.add(rap);
            }
            helper.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    /**
     * Phuong Thuc Lay Thong Tin Rap
     * @param maRap
     * @return
     */
    public RapPOJO layThongTinRap(String maRap) {
        RapPOJO rap = null;
        String sql = String.format("SELECT * FROM Rap WHERE idRap=%d", maRap);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                rap = new RapPOJO();
                rap.setMaRap(rs.getInt("idRap"));
                rap.setTenRap(rs.getString("TenRap"));
                rap.setDiaChi(rs.getString("DiaChi"));
                rap.setSoLuongPhong(rs.getInt("SoLuongPhong"));
            }
            helper.close();
        } catch (SQLException ex) {

            System.out.println(ex.getMessage());
        }
        return rap;
    }

    public ArrayList<RapPOJO> timKiemRap_TheoTenRap(String _tenRap) {

        ArrayList<RapPOJO> ds = new ArrayList<RapPOJO>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            Connection cn = (Connection) helper.getConnection();
            CallableStatement statement = (CallableStatement) cn.prepareCall("{call sp_LayRapTheoTenRap(?)}");
            statement.setString(1, _tenRap);
            statement.execute();
            ResultSet rs = statement.getResultSet();
            while (rs.next()) {
                RapPOJO rap = new RapPOJO();
                rap.setMaRap(rs.getInt("idRap"));
                rap.setTenRap(rs.getString("TenRap"));
                rap.setDiaChi(rs.getString("DiaChi"));
                rap.setSoLuongPhong(rs.getInt("SoLuongPhong"));
                ds.add(rap);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return ds;
    }
}
